/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.skycode.core.hibernate.dao;

import com.skycode.core.exception.DAOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author SkyCode (skycode@gmail.com)
 * @since 07/02/2014
 * @version 1.0
 */
@Repository("daoGenericoCore")
public class DAOSkycodeCore<Dto> extends HibernateDaoSupport implements DAOSkycode<Dto>, Serializable {

    private static final long serialVersionUID = 7924055871810120486L;

    @Autowired
    public DAOSkycodeCore(@Qualifier("sessionFactory") SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public void saveOrUpdate(Dto dto) throws DAOException {
        super.getHibernateTemplate().saveOrUpdate(dto);
        super.getHibernateTemplate().flush();
    }

    @Override
    public void save(Dto dto) throws DAOException {
        super.getHibernateTemplate().save(dto);
        super.getHibernateTemplate().flush();
    }

    @Override
    public void update(Dto dto) throws DAOException {
        super.getHibernateTemplate().update(dto);
        super.getHibernateTemplate().flush();
    }

    @Override
    public void delete(Dto dto) throws DAOException {
        super.getHibernateTemplate().delete(dto);
        super.getHibernateTemplate().flush();
    }

    @Override
    public Dto obtenerDtoPorId(Class c, Serializable id) throws DAOException {
        return (Dto) super.getHibernateTemplate().get(c, id);
    }

    @Override
    public void guardarColeccion(Collection<?> collection) throws DAOException {
        super.getHibernateTemplate().saveOrUpdateAll(collection);
    }

    @Override
    public void borrarColeccion(Collection<?> collection) throws DAOException {
        super.getHibernateTemplate().deleteAll(collection);
    }

    @Override
    public List<Dto> ejecutarQuery(String query) throws DAOException {
        return (List<Dto>) super.getHibernateTemplate().find(query);
    }

    @Override
    public List<Dto> listarTodosDtos(Class<Dto> entityClass) throws DAOException {
        Session session = null;
        List<Dto> lista = null;
        try {
            session = super.getHibernateTemplate().getSessionFactory().openSession();
            Query query = session.createQuery("from "
                    + entityClass.getSimpleName() + " as e");
            lista = (List<Dto>) query.list();

        } finally {
            session.close();
        }
        return lista;
    }

    @Override
    public void refresh(Dto dto) throws DAOException {
        Session session = null;
        session = super.getHibernateTemplate().getSessionFactory().openSession();
        session.refresh(dto);

        session.close();
    }

    @Override
    public void flush() throws DAOException {
        Session session = null;
        session = super.getHibernateTemplate().getSessionFactory().openSession();
        session.flush();
        session.close();
    }
}
